﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExpenseTracker.Entity;
using ExpenseTracker.Controller;

namespace ExpenseTracker
{
    public partial class ExpenseList : BaseForm
    {
        private ExpenseTracker.Controller.CategoryController _categoryController;
        private ExpenseTracker.Controller.ExpenseController _expenseController;
        private Dictionary<int, ExpenseTracker.Entity.Category> _categoryDictionary;

        public ExpenseList(ExpenseTracker.Entity.Account account)
            : base(account)
        {
            _categoryController = new CategoryController(account);
            _expenseController = new ExpenseController(account);
            SetupCategoryDictionary();
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            SetupExpenseList();
            PopulateExpenseList();
            base.OnLoad(e);
        }

        protected void SetupCategoryDictionary()
        {
            _categoryDictionary = new Dictionary<int, Category>();
            List<Category> list = _categoryController.GetCategories(TransactionType.Expense);
            foreach (Category item in list)
            {
                _categoryDictionary.Add(item.CategoryID, item);
            }
        }

        protected void SetupExpenseList()
        {
            dgvExpense.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvExpense.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dgvExpense.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgvExpense.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgvExpense.GridColor = Color.Black;
            dgvExpense.RowHeadersVisible = false;

            dgvExpense.ColumnCount = 7;
            dgvExpense.Columns[0].Name = "Date";
            dgvExpense.Columns[1].Name = "Category";
            dgvExpense.Columns[2].Name = "Item";
            dgvExpense.Columns[3].Name = "Amount";
            dgvExpense.Columns[4].Name = "GST";
            dgvExpense.Columns[5].Name = "PST";
            dgvExpense.Columns[6].Name = "Total";

            dgvExpense.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvExpense.MultiSelect = false;
            dgvExpense.Dock = DockStyle.Fill;

            dgvExpense.CellDoubleClick += new DataGridViewCellEventHandler(dgvExpense_CellDoubleClick);
        }

        public void PopulateExpenseList()
        {
            dgvExpense.Rows.Clear();
            List<Expense> list = _expenseController.GetExpenses();
            int idx = 0;
            float amount = 0.0F;
            float gst = 0.0F;
            float pst = 0.0F;
            float total = 0.0F;

            foreach (Expense item in list)
            {
                amount += item.Amount;
                gst += item.GST;
                pst += item.PST;
                total += item.Total;

                string[] row = { item.TransactionDate.ToShortDateString(), _categoryDictionary[item.CategoryID].Title, item.Item, item.Amount.ToString("N2"), item.GST.ToString("N2"), item.PST.ToString("N2"), item.Total.ToString("N2") };
                dgvExpense.Rows.Add(row);
                dgvExpense.Rows[idx++].Tag = item.ExpenseID;
            }

            Workspace parent = this.MdiParent as Workspace;
            if (parent != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Expense Summary - ");
                sb.Append("Item: [" + list.Count + "] ; ");
                sb.Append("Amount: [" + amount.ToString("N2") + "] ; ");
                sb.Append("GST: [" + gst.ToString("N2") + "] ; ");
                sb.Append("PST: [" + pst.ToString("N2") + "] ; ");
                sb.Append("Total: [" + total.ToString("N2") + "]");
                parent.StatusLabel = sb.ToString();
            }
        }

        protected void dgvExpense_CellDoubleClick(object sender, EventArgs e)
        {
            if (dgvExpense.CurrentRow != null)
            {
                int expenseID = -1;
                if (int.TryParse(dgvExpense.CurrentRow.Tag.ToString(), out expenseID))
                {
                    ExpenseForm childForm = new ExpenseForm(Account);
                    childForm.Name = Guid.NewGuid().ToString();
                    childForm.MdiParent = this.MdiParent;
                    childForm.Text = "Update Expense";
                    childForm.FormAction = FormAction.Update;
                    childForm.Expense.ExpenseID = expenseID;
                    childForm.OnExpenseFormSaved += new ExpenseForm.ExpenseFormSaved(PopulateExpenseList);
                    childForm.Show();
                }
            }
        }

        private void ExpenseList_FormClosing(object sender, FormClosingEventArgs e)
        {
            Workspace parent = this.MdiParent as Workspace;
            if (parent != null)
            {
                parent.StatusLabel = string.Empty;
            }
        }
    }
}
